<?php
if (!isset($_SERVER))
{
   $_GET    = &$HTTP_GET_VARS;
   $_POST    = &$HTTP_POST_VARS;
   $_ENV    = &$HTTP_ENV_VARS;
   $_SERVER  = &$HTTP_SERVER_VARS;
   $_COOKIE  = &$HTTP_COOKIE_VARS;
   $_REQUEST = array_merge($_GET, $_POST, $_COOKIE);
}

$path = $_SERVER['DOCUMENT_ROOT'].'/app/';

$path_to_file = $_SERVER['DOCUMENT_ROOT'].$_GET[path_to_file];
$t = $_REQUEST['t'];

//CONFIGURE HERE ====================================================================
$base_dir = "/spherus_framework/core/classes/SpherusFileEditor/htmleditor/";
if (!$password) $password= ""; //can leave password as "" if you are using .htaccess to protect abitwhizzy
if (!$whizzywig) $whizzywig = $base_dir."whizzywig.js"; //path to whizzywig.js required
if (!$cssFile) $cssFile= $base_dir."simple.css"; //choose your stylesheet, or set to ""
if (!$buttonPath) $buttonPath = $base_dir."whizzery/buttons/"; //toolbar images live here. "" for text buttons
if (!$imageBrowse) $imageBrowse = $base_dir."whizzery/whizzypic.php"; // "" for no image browser
if (!$linkBrowse) $linkBrowse = $base_dir."whizzery/whizzylink.php"; // "" for no link browser
if (!$toolbar) $toolbar = "all"; //try "all" if you need more
if (!$editarea) $editarea = "width:100%; height:550px"; 
if (!$xhtml) $xhtml = ""; //path to xhtml converter, or set to "" for HTML 4.01. Use appropriate header in $top.
if (!$extensions) $extensions = "/(html|htm)$/"; //file extensions to consider for edit (in the brackets|to separate)

if ($_REQUEST['save']) { //write page and go there
 $edited = $_REQUEST['edited'];
 if ($f == "top.shtml" || $f == "tail.shtml") $html = stripslashes($edited);
 else {
  $html .= stripslashes($edited);
 }

 $fsave = fopen($path_to_file, 'w+');  //save the edited file
 fwrite($fsave, $html);
 fclose($fsave);
 header("Location: {$_SERVER[HTTP_REFERER]}");
} else if (file_exists($path_to_file)&&(ereg("(html|htm|tpl)$", pathinfo($path_to_file, PATHINFO_EXTENSION)))) {
 $fedit = fopen($path_to_file, 'r');  //open the file for edit
 $content = fread($fedit, filesize($path_to_file));
 $start = strpos($content,"<!--#include virtual='top.shtml'-->") ? "/.*<!--#include virtual='top.shtml'-->/" : "|.*<body[^>]*>|iU";
 $stop = strpos($content,"<!--#include virtual='tail.shtml'-->") ? "/<!--#include virtual='tail.shtml'-->.*/" : "|</body>.*$|iU";
 $content = preg_replace($start,'',$content);
 $content = preg_replace($stop,'',$content);
 fclose($fedit); 
 preg_match('|<title>(.*)</title>|i',$content, $match);
 $t = $match[1];  //grab the title
} 

//form
?>
<html>
<head>
 <meta http-equiv = 'Content-Type' content = 'text/html; charset=windows-1251' >
 <link type="text/css" rel="stylesheet" href="<?php echo $cssFile ?>" />
 <title>Content manager</title>
 <script language="JavaScript" type="text/javascript" src="<?= $whizzywig ?>"></script>
 <? if ($xhtml) echo '<script language="JavaScript" type="text/javascript" src="'.$xhtml.'>"></script>'; ?>
 <meta name="robots" content="noindex,nofollow">
</head>
<body>
<form name="Whizzy" action="<? $_SERVER['SCRIPT_NAME']; ?>" method="post" onSubmit="return insistF()">
<input name="f" id="f" type="hidden" value="<?=$path_to_file;?>">
<textarea name="edited" id="edited" rows="25" cols="70" style="<?=$editarea;?>"><?=$content;?></textarea>
<script language="JavaScript" type="text/javascript">
 buttonPath = "<?=$buttonPath;?>";
 cssFile = "<?=$cssFile;?>"
 imageBrowse = "<?=$imageBrowse;?>";
 linkBrowse = "<?=$linkBrowse;?>";
 makeWhizzyWig("edited", "<?=$toolbar;?>");
</script>
</form>
</body>
</html>
